﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NAccounting.Models
{
    public enum FilterDateOptionsEnum
    {
        Undefined,
        Yesterday,
        Today,
        Tomorrow,
        LastWeek,
        ThisWeek,
        NextWeek,
        LastMonth,
        ThisMonth,
        NextMonth,
        LastQuartile,
        ThisQuartile,
        NextQuartile,
        LastSemester,
        ThisSemester,
        NextSemester
    }

    public class FilterDateOptions
    {
        public IEnumerable<FilterDateOptionsItem> Options { get; set; }

        public FilterDateOptions()
        {
            Options = new List<FilterDateOptionsItem>(new[] { 
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.Yesterday, Text = "Ontem"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.Today, Text = "Hoje"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.Tomorrow, Text = "Amanhã"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.LastWeek, Text = "Semanha Passada"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.ThisWeek, Text = "Esta Semana"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.NextWeek, Text = "Próxima Semana"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.LastMonth, Text = "Mês Passado"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.ThisMonth, Text = "Este Mês"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.NextMonth, Text = "Próximo Mês"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.LastQuartile, Text = "Quartil Passado"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.ThisQuartile, Text = "Este Quartil"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.NextQuartile, Text = "Próximo Quartil"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.LastSemester, Text = "Semestre Passado"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.ThisSemester, Text = "Este Semestre"},
                new FilterDateOptionsItem{ Key = FilterDateOptionsEnum.NextSemester, Text = "Próximo Semestre"},
            });
        }

        public static TimePeriod GetPeriodByDateOption(string option)
        { 
            FilterDateOptionsEnum dateOption = FilterDateOptionsEnum.Undefined;
            if (!Enum.TryParse<FilterDateOptionsEnum>(option, out dateOption))
                throw new ArgumentException("Option is not filter date option value");

            var result =
                dateOption == FilterDateOptionsEnum.Yesterday ? TimePeriod.CreatePeriodFor(Calendar.Yesterday()) :
                dateOption == FilterDateOptionsEnum.Today ? TimePeriod.CreatePeriodFor(DateTime.Now) :
                dateOption == FilterDateOptionsEnum.Tomorrow ? TimePeriod.CreatePeriodFor(Calendar.Tomorrow()) :
                dateOption == FilterDateOptionsEnum.LastWeek ? Calendar.LastWeek() :
                dateOption == FilterDateOptionsEnum.ThisWeek ? Calendar.ThisWeek() :
                dateOption == FilterDateOptionsEnum.NextWeek ? Calendar.NextWeek() :
                dateOption == FilterDateOptionsEnum.LastMonth ? Calendar.LastMonth() :
                dateOption == FilterDateOptionsEnum.ThisMonth ? Calendar.ThisMonth() :
                dateOption == FilterDateOptionsEnum.NextMonth ? Calendar.NextMonth() :
                dateOption == FilterDateOptionsEnum.LastQuartile ? Calendar.LastQuartile() :
                dateOption == FilterDateOptionsEnum.ThisQuartile ? Calendar.ThisQuartile() :
                dateOption == FilterDateOptionsEnum.NextQuartile ? Calendar.NextQuartile() :
                dateOption == FilterDateOptionsEnum.LastSemester ? Calendar.LastSemester() :
                dateOption == FilterDateOptionsEnum.ThisSemester ? Calendar.ThisSemester() : Calendar.NextSemester();

            return result;
        }

        public class FilterDateOptionsItem {
            public FilterDateOptionsEnum Key { get; set; }
            public string Text { get; set; }
        }
    }
}